﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Galaktika.BI.Runtime.Services;
using Galaktika.BI.Silverlight.ClientApp.Web.Runtime;
using Galaktika.BI.Silverlight.Services;
using Galaktika.BI.Silverlight.Commands;

namespace Galaktika.BI.Silverlight.ClientApp.Web.Commands
{
    public class FunctionCallCommand : ServerCommand
    {
        public FunctionCallCommand(InvokeSchema invoker)
            : base(invoker.CommandId)
        {
            this.ContextId = invoker[KnownInvokeArgs.ContextId];
            this.FunctionName = invoker[KnownInvokeArgs.FunctionName];
            if (this.ContextId == null)
            {
                throw new ArgumentNullException(KnownInvokeArgs.ContextId);
            }
            if (this.FunctionName == null)
            {
                throw new ArgumentNullException(KnownInvokeArgs.FunctionName);
            }
            this.Parameters = new List<object>();
            for (int i = 0; ; i++)
            {
                string argName = KnownInvokeArgs.ArgumentName + i.ToString();
                string arg = invoker[argName];
                if (arg == null) break;

                this.Parameters.Add(arg);

            }
        }

        public string ContextId { get; private set; }
        public string FunctionName { get; private set; }
        public List<object> Parameters { get; private set; }

        public override InvokeResultSchema Invoke()
        {
            InvokeResultSchema res = new InvokeResultSchema(base.Id);
            res.ContentType = KnownContentTypes.Error;

            Type type = ClientResources.ClientAssembly.GetType(this.FunctionName);
            if (type == null)
            {
                res.Content = string.Format(Localization.Msg_FunctionNotFound, this.FunctionName);
                return res;
            }

            MethodInfo mi = type.GetMethod("Invoke", BindingFlags.Static | BindingFlags.Public);
            if (mi == null)
            {
                res.Content = string.Format(Localization.Msg_InvokeNotFound, this.FunctionName);
                return res;
            }

            this.SetCurrentBindingContext();

            res.ContentType = KnownContentTypes.String;
            object invokeResult = mi.Invoke(null, this.Parameters.ToArray());
            if (invokeResult != null)
            {
                res.Content = invokeResult.ToString();
            }

            return res;
        }

        private void SetCurrentBindingContext()
        {
            DataBinder dataBinder = DataBinder.Current;
            if (dataBinder != null)
            {
                //ServerResources.ServerAssembly
            }
        }
    }
}
